$(function () {

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            loginName: function(value){
                if(value == ''){
                    return '登录名不能为空';
                }
            },
            loginName: function(value){
                if(value == ''){
                    return '登录名不能为空';
                }
            },
            roleIds: function(value){
                var checked = $("input[type='checkbox']").is(':checked');
                if(!checked){
                    return '请至少选择一个角色';
                }
            },
            //password: [/(.+){6,12}$/, '密码必须6到12位'],
            password: function(value){
                if($('input[name=id]').val() && value == ''){
                    return;
                }
                if(!/(.+){6,12}$/.test(value)){
                    return '密码必须6到12位';
                }
            },
            rePassword: function(value){
                if($('input[name=id]').val() && $('#password').val() == ''){
                   return;
                }
                if($('#password').val() != $('#rePassword').val()){
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            //console.log(data);
            //发异步，把数据提交给php
            $.ajax({
                url:save_url,
                data:data.field,
                type:"Post",
                dataType:"json",
                success:function(data){
                    if(data.status == 1){
                        layer.msg("添加成功");
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function(data){
                    layer.msg("服务器无响应");
                }
            });

            return false;
        });
    });

});